<?php
class RatesController extends AppController {

    var $name = 'Rates';
    var $components = array('Json');

    function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('add');
    }

    /**
     * Add Rating from Viewing Post
     * usage in : /posts/view.ctp
     */
    function add(){
        $this->layout = 'ajax';
        Configure::write('debug',0);
        App::import('Sanitize');

        /* Defining tags array variables from ajax input */
        $slug = Sanitize::escape($this->params['form']['post']);
        $ip = Sanitize::escape($this->params['form']['user']);
        
        /* Checking slug is exist or not */
        $post_id = $this->Rate->Post->checkPostBySlug($slug);
        if(empty($post_id)){ $this->Json->error('1'); }

        /* Checking visitors have rated same logo in 90 minutes before */
        $checkRate = $this->Rate->checkRate($post_id['Post']['post_id'],$ip);
        
        if(!empty($checkRate)){
            $this->Json->error('0');
        }else{
            $rate = $this->Rate->updateRate($post_id['Post']['post_id'],$ip);
            $this->Json->result($rate);
        }
    }

}
?>